package com.water.app.Controller.account;

import com.water.app.Result;
import com.water.app.User;
import com.water.app.interfaces.account.EmailService;
import com.water.app.interfaces.account.RegisterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/forget")
public class ForgetController {


    private static final Logger log= LoggerFactory.getLogger(ForgetController.class);

    @Autowired
    private RegisterService registerService;

    @Autowired
    private EmailService emailService;

    @PostMapping("/c1")
    public Result forget1(@RequestBody User user){
        log.info("user:{}",user);
        if(user==null) {
            log.info("未收到user对象");
            return Result.error("未收到user对象");
        }
        if(registerService.verifyAccount(user.getAccount())){
            log.info("账号已存在");
            return Result.error("账号已存在");
        }

        if(registerService.verifyUsername(user.getUsername())){
            log.info("用户名已经存在");
            return Result.error("用户名已经存在");
        }

        if(!user.getPassword().equals(user.getConfirmPassword())){
            log.info("两次密码输入不一致");
            return Result.error("两次密码输入不一致");
        }

        int captcha=emailService.sendEmail(user.getEmail());

        return Result.success(captcha);
    }

    @PostMapping("/c2")
    public Result forget2(@RequestBody User user) {
        if(user==null){
            log.info("用户user未收到");
            return Result.error("用户user未收到");
        }

        //redis判断验证码是否超时


        if(!registerService.reviseUser(user)){
            log.info("重置密码失败");
            return Result.error("重置密码失败");
        }
        log.info("重置密码成功");
        return Result.success("重置密码成功");

    }


}
